﻿using System.Collections.Generic;
using System.Linq;

namespace KingerRobot.MexicoSailunMDC.Dao;
public static class PathHelper
{
    private static List<POWcsNetAbnormal> s_AbnormalPaths = new List<POWcsNetAbnormal>();
    private static List<POWcsAsrsNetOutput> s_NormalPaths = new List<POWcsAsrsNetOutput>();

    static PathHelper()
    {
        s_AbnormalPaths = Db.Default.WcsNetAbnormals.ToList();
        s_NormalPaths = Db.Default.WcsAsrsNetOutputs.ToList();
    }

    public static POWcsNetAbnormal GetAbnormalPath(long startLoc)
    {
        return s_AbnormalPaths.Single(item => item.UseFlag == 1 && item.StartLoc == startLoc);
    }

    public static List<POWcsNetAbnormal> GetAbnormalPathsOfAngleScanLoc(long startLoc)
    {
        return s_AbnormalPaths.Where(item => item.UseFlag == 1 && item.StartLoc == startLoc).OrderByDescending(item => item.RelateScore).ToList();
    }

    public static POWcsAsrsNetOutput GetNormalPath(long startLoc, long targetLoc)
    {
        return GetNormalPath(item => item.TranLoc == startLoc && item.TargetLoc == targetLoc);
    }

    //public static POWcsAsrsNetOutput GetNormalFromAnglePath(long angleLoc, long targetLoc)
    //{
    //    return GetNormalPath(item => item.AngleScanLoc == angleLoc && item.TargetLoc == targetLoc);
    //}

    public static POWcsAsrsNetOutput GetNormalPath(Func<POWcsAsrsNetOutput, bool> predicate)
    {
        return s_NormalPaths.Single(item => item.UseFlag == 1 && predicate(item));
    }

    //public static POWcsAsrsNetOutput GetNormalPathOfEmsPickLoc(long startLoc, long targetLoc, int emsNo)
    //{
    //    var query = s_NormalPaths.Where(item => item.TargetLoc == targetLoc);
    //    if (emsNo == 1)
    //    {
    //        query = query.Where(item => item.EmsPickLoc1 == startLoc);
    //    }
    //    else if (emsNo == 2)
    //    {
    //        query = query.Where(item => item.EmsPickLoc2 == startLoc);
    //    }
    //    else
    //    {
    //        return null;
    //    }
    //    return query.Single();
    //}

    //public static bool CheckIfTaskPathInvalid(TaskAndCmd taskAndCmd, int emsNo, out string error)
    //{
    //    error = null;

    //    if (taskAndCmd.Task.IsAbnormal.Value)
    //    {
    //        var abnormalPath = PathHelper.GetAbnormalPath(taskAndCmd.TaskCmd.StartLoc.Value);
    //        if (abnormalPath == null)
    //        {
    //            error = $"未找到开始位为{taskAndCmd.TaskCmd.StartLoc.Value}的异常路径";
    //        }

    //        if (abnormalPath.TargetLoc != taskAndCmd.TaskCmd.TargetLoc)
    //        {
    //            error = $"异常路径目标位跟指令目标位不匹配。路径目标位：{abnormalPath.TargetLoc}，指令目标位：{taskAndCmd.TaskCmd.TargetLoc}";
    //        }
    //    }
    //    else
    //    {
    //        var normalPath = PathHelper.GetNormalPathOfEmsPickLoc(taskAndCmd.TaskCmd.StartLoc.Value, taskAndCmd.TaskCmd.TargetLoc.Value, emsNo);
    //        if (normalPath == null)
    //        {
    //            error = $"未找到开始位为{taskAndCmd.TaskCmd.StartLoc.Value}，目标位为{taskAndCmd.TaskCmd.TargetLoc.Value}的正常路径";
    //        }
    //    }

    //    return !string.IsNullOrWhiteSpace(error);
    //}
}
